package com.xyh.cloudorder.service.order.impl;

import com.xyh.cloudorder.mapper.order.CloOrderMapper;
import com.xyh.cloudorder.mapper.order.CloOrderStatusMapper;
import com.xyh.cloudorder.mapper.order.CloOrdermxMapper;
import com.xyh.cloudorder.pojo.order.CloOrder;
import com.xyh.cloudorder.pojo.order.CloOrderStatus;
import com.xyh.cloudorder.pojo.order.CloOrdermx;
import com.xyh.cloudorder.pojo.order.param.*;
import com.xyh.cloudorder.service.order.IOrder;
import com.xyh.cloudorder.utils.Convert;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderImpl implements IOrder {

    @Autowired
    private CloOrderMapper cloOrderMapper;

    @Autowired
    private CloOrdermxMapper cloOrdermxMapper;

    @Autowired
    private CloOrderStatusMapper cloOrderStatusMapper;

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void newOrder(newOrderParam param) throws Exception {
        String ordercode = param.getOrdercode();
        String hzname = param.getHzname();
        String sex = param.getSex();
        String idcard = param.getIdcard();
        String hzlx = param.getHzlx();
        String icdname = param.getIcdname();
        String allje = param.getAllje();
        String bzje = param.getBzje();
        String zfje = param.getZfje();
        String forgid = param.getForgid();
        String frcode = param.getFrcode();

        //验证订单是否重复
        CloOrder cloOrder1 = new CloOrder();
        cloOrder1.setOrdercode(ordercode);
        cloOrder1.setForgid(Convert.toBigDecimal(forgid));
        cloOrder1.setFrcode(frcode);
        CloOrder result = cloOrderMapper.selectOne(cloOrder1);
        if(result != null){
            throw new RuntimeException("订单号重复！");
        }

        CloOrder cloOrder = new CloOrder();
        cloOrder.setOrdercode(ordercode);
        cloOrder.setHzname(hzname);
        cloOrder.setSex(sex);
        cloOrder.setIdcard(idcard);
        cloOrder.setHzlx(hzlx);
        cloOrder.setIcdname(icdname);
        cloOrder.setAllje(Convert.toBigDecimal(allje));
        cloOrder.setBzje(Convert.toBigDecimal(bzje));
        cloOrder.setZfje(Convert.toBigDecimal(zfje));
        cloOrder.setIsfp(Convert.toBigDecimal("0"));
        cloOrder.setStatus(Convert.toBigDecimal("0"));
        cloOrder.setForgid(Convert.toBigDecimal(forgid));
        cloOrder.setFrcode(frcode);
        cloOrder.setOperdate(new DateTime().toDate());
        cloOrder.setPsaddr("");
        cloOrder.setPslxr("");
        cloOrder.setPstel("");
        cloOrder.setBz1("");
        cloOrder.setBz2("");
        cloOrder.setBz3("");
        cloOrder.setBz4("");
        cloOrderMapper.insert(cloOrder);

        List<CloOrdermx> cloOrdermxList = param.getCloOrdermxList();
        for (int i = 0; i < cloOrdermxList.size(); i++) {
            CloOrdermx cloOrdermx = cloOrdermxList.get(i);
            CloOrdermx temple = new CloOrdermx();

            if(cloOrdermx.getMediname() == null){
                throw new RuntimeException("药品名称为空！");
            }
            String mediname = cloOrdermx.getMediname();
            if(cloOrdermx.getSerial() == null){
                throw new RuntimeException(mediname + "的序号为空！");
            }
            if(cloOrdermx.getUnitprice() == null){
                throw new RuntimeException(mediname + "的单价为空！");
            }
            if(cloOrdermx.getUnit() == null){
                throw new RuntimeException(mediname + "的单位为空！");
            }
            if(cloOrdermx.getQuantity() == null){
                throw new RuntimeException(mediname + "的数量为空！");
            }
            if(cloOrdermx.getItemamou() == null){
                throw new RuntimeException(mediname + "的金额为空！");
            }

            //开始组织插入数据库的实体类
            temple.setOrdercode(ordercode);
            temple.setSerial(cloOrdermx.getSerial());
            temple.setZh(cloOrdermx.getZh());
            temple.setMediname(cloOrdermx.getMediname());
            temple.setSpec(cloOrdermx.getSpec());
            temple.setUnitprice(cloOrdermx.getUnitprice());
            temple.setUnit(cloOrdermx.getUnit());
            temple.setQuantity(cloOrdermx.getQuantity());
            temple.setItemamou(cloOrdermx.getItemamou());
            if(cloOrdermx.getUsagename() == null){
                temple.setUsagename("");
            }else{
                temple.setUsagename(cloOrdermx.getUsagename());
            }

            if(cloOrdermx.getPlname() == null){
                temple.setPlname("");
            }else{
                temple.setPlname(cloOrdermx.getPlname());
            }

            if(cloOrdermx.getDosage() == null){
                temple.setDosage(Convert.toBigDecimal("0"));
            }else{
                temple.setDosage(cloOrdermx.getDosage());
            }

            if(cloOrdermx.getYldw() == null){
                temple.setYldw("");
            }else{
                temple.setYldw(cloOrdermx.getYldw());
            }
            temple.setForgid(Convert.toBigDecimal(forgid));
            temple.setFrcode(frcode);
            cloOrdermxMapper.insert(temple);
        }

        //此处应该插入订单更新信息表一条信息
        CloOrderStatus cloOrderStatus = new CloOrderStatus();
        cloOrderStatus.setOrdercode(ordercode);
        cloOrderStatus.setXh(Convert.toBigDecimal("1"));
        cloOrderStatus.setOldstatus(Convert.toBigDecimal("0"));
        cloOrderStatus.setNewstatus(Convert.toBigDecimal("0"));
        cloOrderStatus.setBgdate(new DateTime().toDate());
        cloOrderStatus.setBgly("");
        cloOrderStatus.setBgopername("");
        cloOrderStatus.setForgid(Convert.toBigDecimal(forgid));
        cloOrderStatus.setFrcode(frcode);
        cloOrderStatusMapper.insert(cloOrderStatus);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void updateOrderStatus(updateOrderStatusParam updateOrderStatusParam) throws Exception {
        //首先判断订单是否存在
        CloOrder cloOrder = new CloOrder();
        cloOrder.setForgid(Convert.toBigDecimal(updateOrderStatusParam.getForgid()));
        cloOrder.setFrcode(updateOrderStatusParam.getFrcode());
        cloOrder.setOrdercode(updateOrderStatusParam.getOrdercode());
        CloOrder cloOrder1 = cloOrderMapper.selectOne(cloOrder);
        if(cloOrder1 == null){
            throw new RuntimeException("此订单不存在！");
        }

        //判断订单状态信息表是否存在
        CloOrderStatus cloOrderStatus = new CloOrderStatus();
        cloOrderStatus.setForgid(Convert.toBigDecimal(updateOrderStatusParam.getForgid()));
        cloOrderStatus.setFrcode(updateOrderStatusParam.getFrcode());
        cloOrderStatus.setOrdercode(updateOrderStatusParam.getOrdercode());
        List<CloOrderStatus> select = cloOrderStatusMapper.select(cloOrderStatus);
        if(select.size() == 0){
            throw new RuntimeException("此订单状态明细信息不存在！");
        }

        //取出原来的状态
        String oldstatus = cloOrder1.getStatus().toString();
        if(oldstatus.equals(updateOrderStatusParam.getStatus())){
            throw new RuntimeException("此订单目前的状态和要更新的状态相同！");
        }

        //更新主表的状态
        CloOrder updateOrder = new CloOrder();
        Example updateOrderStatusExample = new Example(CloOrderStatus.class);
        Example.Criteria criteria = updateOrderStatusExample.createCriteria();
        criteria.andEqualTo("frcode",updateOrderStatusParam.getFrcode());
        criteria.andEqualTo("forgid", updateOrderStatusParam.getForgid());
        criteria.andEqualTo("ordercode", updateOrderStatusParam.getOrdercode());
        updateOrder.setStatus(Convert.toBigDecimal(updateOrderStatusParam.getStatus()));
        int i = cloOrderMapper.updateByExampleSelective(updateOrder,updateOrderStatusExample);
        if(i != 1){
            throw new RuntimeException("更新此订单主表状态失败！");
        }

        //插入状态明细表信息
        CloOrderStatus updateStatus = new CloOrderStatus();
        updateStatus.setOrdercode(updateOrderStatusParam.getOrdercode());
        updateStatus.setForgid(Convert.toBigDecimal(updateOrderStatusParam.getForgid()));
        updateStatus.setFrcode(updateOrderStatusParam.getFrcode());
        //确定序号
        int xh = select.size() + 1;
        updateStatus.setXh(Convert.toBigDecimal(xh + ""));
        updateStatus.setOldstatus(Convert.toBigDecimal(oldstatus));
        updateStatus.setNewstatus(Convert.toBigDecimal(updateOrderStatusParam.getStatus()));
        updateStatus.setBgdate(new DateTime().toDate());
        if(updateOrderStatusParam.getBgly() == null){
            updateStatus.setBgly("");
        }else{
            updateStatus.setBgly(updateOrderStatusParam.getBgly());
        }
        if(updateOrderStatusParam.getBgopername() == null){
            updateStatus.setBgopername("");
        }else{
            updateStatus.setBgopername(updateOrderStatusParam.getBgopername());
        }
        cloOrderStatusMapper.insert(updateStatus);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void updateOrderOther(updateOrderOtherParam param) throws Exception {
        String ordercode = param.getOrdercode();
        String forgid = param.getForgid();
        String frcode = param.getFrcode();

        String isfp = param.getIsfp();
        String bxje = param.getBxje();
        String zfje = param.getZfje();
        String psaddr = param.getPsaddr();
        String pslxr = param.getPslxr();
        String pstel = param.getPstel();
        String sfid = param.getSfid();
        String psinfo = param.getPsinfo();

        //查询订单主信息是否存在
        CloOrder cloOrder = new CloOrder();
        cloOrder.setOrdercode(ordercode);
        cloOrder.setFrcode(frcode);
        cloOrder.setForgid(Convert.toBigDecimal(forgid));
        CloOrder cloOrder1 = cloOrderMapper.selectOne(cloOrder);
        if(cloOrder1 == null){
            throw new RuntimeException("未查询到此订单！");
        }

        CloOrder updatecloOrder = new CloOrder();
        Example updatecloOrderExample = new Example(CloOrder.class);
        Example.Criteria criteria = updatecloOrderExample.createCriteria();
        criteria.andEqualTo("frcode",frcode);
        criteria.andEqualTo("forgid", forgid);
        criteria.andEqualTo("ordercode", ordercode);
        if(bxje != null && zfje != null){
            //更新金额  判断金额是否正确
            BigDecimal allje = cloOrder1.getAllje();
            BigDecimal bxje1 = new BigDecimal(bxje);
            BigDecimal zfje1 = new BigDecimal(zfje);
            if(bxje1.add(zfje1).compareTo(allje) != 0){
                throw new RuntimeException("总金额不等于自费金额与报销金额之和！");
            }
            updatecloOrder.setBzje(Convert.toBigDecimal(bxje));
            updatecloOrder.setZfje(Convert.toBigDecimal(zfje));
        }
        if(isfp != null){
            updatecloOrder.setIsfp(Convert.toBigDecimal(isfp));
        }
        if(psaddr != null){
            updatecloOrder.setPsaddr(psaddr);
        }
        if(pslxr != null){
            updatecloOrder.setPslxr(pslxr);
        }
        if(pstel != null){
            updatecloOrder.setPstel(pstel);
        }
        if(sfid != null){
            updatecloOrder.setBz1(sfid);
        }
        if(psinfo != null){
            updatecloOrder.setBz2(psinfo);
        }
        cloOrderMapper.updateByExampleSelective(updatecloOrder,updatecloOrderExample);
    }

    @Override
    public Map queryOrderAllInfo(queryOrderAllInfoParam param) throws Exception {
        String ordercode = param.getOrdercode();
        String forgid = param.getForgid();
        String frcode = param.getFrcode();

        CloOrder cloOrder = new CloOrder();
        cloOrder.setForgid(Convert.toBigDecimal(forgid));
        cloOrder.setFrcode(frcode);
        cloOrder.setOrdercode(ordercode);

        CloOrder cloOrder1 = cloOrderMapper.selectOne(cloOrder);
        if(cloOrder1 == null){
            throw new RuntimeException("未查询到此订单！");
        }

        CloOrdermx cloOrdermx = new CloOrdermx();
        cloOrdermx.setFrcode(frcode);
        cloOrdermx.setForgid(Convert.toBigDecimal(forgid));
        cloOrdermx.setOrdercode(ordercode);
        List<CloOrdermx> select = cloOrdermxMapper.select(cloOrdermx);

        Map returnMap = new HashMap();
        returnMap.put("order",cloOrder1);
        returnMap.put("ordermx",select);
        return returnMap;
    }

    @Override
    public Map queryOrderAllInfoByBz1(queryOrderAllInfoByBz1Param param) throws Exception {
        String bz1 = param.getBz1();
        CloOrder cloOrder = new CloOrder();
        cloOrder.setBz1(bz1);
        CloOrder cloOrder1 = cloOrderMapper.selectOne(cloOrder);
        if(cloOrder1 == null){
            throw new RuntimeException("未查询到此订单！");
        }
        CloOrdermx cloOrdermx = new CloOrdermx();
        cloOrdermx.setFrcode(cloOrder1.getFrcode());
        cloOrdermx.setForgid(cloOrder1.getForgid());
        cloOrdermx.setOrdercode(cloOrder1.getOrdercode());
        List<CloOrdermx> select = cloOrdermxMapper.select(cloOrdermx);
        Map returnMap = new HashMap();
        returnMap.put("order",cloOrder1);
        returnMap.put("cloOrdermxList",select);
        return returnMap;
    }

    @Override
    public Map queryOrderAllInfoBySfzh(queryOrderAllInfoBySfzhParam param) throws Exception {
        String sfzh = param.getSfzh();
        CloOrder cloOrder = new CloOrder();
        cloOrder.setIdcard(sfzh);
        List<CloOrder> select = cloOrderMapper.select(cloOrder);
        if(select.size() == 0){
            throw new RuntimeException("未查询到订单信息！");
        }
        Map returnMap = new HashMap();
        returnMap.put("orderList",select);
        return returnMap;
    }
}
